package cn.com.sbell.vss.csg.vap.util;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpHost;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.heartbeat.HeartBeatHandler;
import cn.com.sbell.vss.csg.heartbeat.HeartBeatMessage;
import cn.com.sbell.vss.csg.heartbeat.ResponseInfoToVS;
import cn.com.sbell.vss.csg.vsclient.util.AlarmDispatchGroup;
import cn.com.sbell.vss.csg.vsclient.util.CSGConfiguration;

public class VsInfoManager {

	// key: String:vsDeviceId; value:VsInfo
	private Map vsDeviceIdVsInfoMap = Collections.synchronizedMap(new LinkedHashMap());

	private static Log logger = LogFactory.getLog(VsInfoManager.class);

	private HttpClient httpClient;

	private String cmsUrl;
	
	private static VsInfoManager vsInfoManager;
	
	static //deleted vondart
	
	{
		logger.info("Init VsInfoManager ...");
		vsInfoManager = new VsInfoManager();
		logger.info("Init VsInfoManager success:"+vsInfoManager.cmsUrl+"||"+vsInfoManager.httpClient);
	}
	
	private VsInfoManager()
	{
		
	}
//	removed static
	public 	static 	VsInfoManager getVsInfoManager(){
		return vsInfoManager;
	}
	
	public VsInfo getVsInfo(String vsDeviceId,String front_type){
		VsInfo vsInfo = (VsInfo) vsDeviceIdVsInfoMap.get(vsDeviceId);

		if (null != vsInfo) {
			return vsInfo;
		}
		
		NameValuePair[] pairs = new NameValuePair[] { new NameValuePair(
				"vsDeviceId", vsDeviceId) };
		
		PostMethod method = null;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//distinguish vs and ah
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		if(front_type.equalsIgnoreCase(VssConstants.FRONT_TYPE_VS))
			{
			method=new PostMethod(cmsUrl + "/GetVsInfo");
			logger.warn("GetVsInfo from CMS: " + cmsUrl + "/GetVsInfo");
			}
		else if(front_type.equalsIgnoreCase(VssConstants.FRONT_TYPE_AH)){
			method=new PostMethod(cmsUrl+"/GetAhInfo");
			logger.warn("GetAhInfo from CMS: " + cmsUrl + "/GetAhInfo");
			pairs=new NameValuePair[] { new NameValuePair(
					"ahDeviceId", vsDeviceId) };
		}
		method.addParameters(pairs);
		

		try {
			// Execute the method.
			int statusCode = httpClient.executeMethod(method);

			if (HttpStatus.SC_OK != statusCode) {
				logger.warn("PostMethod failed at GetVsInfo: "
						+ method.getStatusLine());
			} else {
				String body = method.getResponseBodyAsString();
				Document doc = DocumentHelper.parseText(body);
				Node returnCodeByEMSNode = doc
						.selectSingleNode("/response/result/@code");
				if (!VssConstants.CODE_SUCCESS.equals(returnCodeByEMSNode
						.getText())) {
					logger.warn("GetVsInfo : returnCodeByEMS ="
							+ returnCodeByEMSNode.getText());
				} else {
					
					Node vsInfoNode =null;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//distinguish between vs and ah
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
					if(front_type.equalsIgnoreCase(VssConstants.FRONT_TYPE_VS)){
						vsInfoNode= doc.selectSingleNode("//vsInfo");
					}else if(front_type.equalsIgnoreCase(VssConstants.FRONT_TYPE_AH))
					{
						vsInfoNode= doc.selectSingleNode("//ahInfo");
					}
					if (null != vsInfoNode) {
						// Node isOnlineNode =
						// doc.selectSingleNode("//isOnline");
						// Node isDisabledNode =
						// doc.selectSingleNode("//isDisabled");
						Node deviceIdNode = doc.selectSingleNode("//deviceId");
						Node ipNode = doc.selectSingleNode("//ip");
						Node managementPortNode = doc
								.selectSingleNode("//managementPort");
						
						if (deviceIdNode != null && ipNode != null
								&& managementPortNode != null) {
							vsInfo = new VsInfo(deviceIdNode.getText(),
									ipNode.getText(),new Integer(managementPortNode.getText()),front_type); 
						} else {
							logger.warn("GetVsInfo : return info by ems error,may be null!");
						}
					}
				}
			}
		} catch (HttpException e) {
			logger.error("GetVsInfo : HttpException!");
			e.printStackTrace();
		} catch (IOException e) {
			logger.error("GetVsInfo : IOException!");
			e.printStackTrace();
		} catch (DocumentException e) {
			logger.error("GetVsInfo : DocumentException!");
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}

		return vsInfo;
	}
	public VsInfo checkVsInfo(String vsDeviceId){
		return (VsInfo) vsDeviceIdVsInfoMap.get(vsDeviceId);
	}
	public void addVsInfo(VsInfo info) {
		vsDeviceIdVsInfoMap.put(info.getVsDeviceId(), info);
	}
	
	public void deleteVsInfo(String vsDeviceId) {
		vsDeviceIdVsInfoMap.remove(vsDeviceId);
	}

	public String getCmsUrl() {
		return cmsUrl;
	}

	public void setCmsUrl(String cmsUrl) {
		this.cmsUrl = cmsUrl;
	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

}